class Solution:
    def longestCommonSubsequence(self, s: str, t: str) -> int:
        n, m = len(s), len(t)
        f = [[0] * (m + 1) for _ in range(2)]
        for i, x in enumerate(s):
            for j, y in enumerate(t):
                f[(i + 1) % 2][j + 1] = f[i % 2][j] + 1 if x == y else max(f[i % 2][j + 1], f[(i + 1) % 2][j])
        return f[n % 2][m]

a=Solution()
print(a.longestCommonSubsequence("abcde", "ace" ));